#!/usr/local/bin/perl -w 
# Translate a DNA sequence in all six open reading frame 
# Import the Bioperl modules 
use Bio::Seq; 
my $seq_obj = Bio::Seq->new(-seq => "ATGCGATGCGTCTAATCGTAGGGGACTAGCTCGTGTCGGTCGC"); 

# Get the reverse complement sequence, $revcom_obj is also an object, not just a string 
$revcom_obj = $seq_obj->revcom; 

# Translate the sequence in first three open reading frames 
$first_frame_obj = $seq_obj->translate(undef, undef, 0); 
$second_frame_obj = $seq_obj->translate(undef, undef, 1); 
$third_frame_obj = $seq_obj->translate(undef, undef, 2); 

# Translate the sequence in the next three open reading frame. 
# Notice we use the reverse complement sequence for this purpose. 
# The available open frames for choosing when call the translate method are only 0, 1, or 2 with 0 being default. 
$fourth_frame_obj = $revcom_obj->translate(undef, undef, 0); 
$fifth_frame_obj = $revcom_obj->translate(undef, undef, 1); 
$sixth_frame_obj = $revcom_obj->translate(undef, undef, 2); 

# Get the translated sequences in strings 
$first_frame_seq = $first_frame_obj->seq(); 
$second_frame_seq = $second_frame_obj->seq(); 
$third_frame_seq = $third_frame_obj->seq(); 
$fourth_frame_seq = $fourth_frame_obj->seq(); 
$fifth_frame_seq = $fifth_frame_obj->seq(); 
$sixth_frame_seq = $sixth_frame_obj->seq(); 

# Print out the proteins sequences in lines 70 characters long 
print "\n------------------------Reading Frame 1---------------------\n\n"; 
print_sequence($first_frame_seq, 70); 
print "\n------------------------Reading Frame 2---------------------\n\n"; 
print_sequence($second_frame_seq, 70); 
print "\n------------------------Reading Frame 3---------------------\n\n"; 
print_sequence($third_frame_seq, 70); 
print "\n------------------------Reading Frame 4---------------------\n\n"; 
print_sequence($fourth_frame_seq, 70); 
print "\n------------------------Reading Frame 5---------------------\n\n"; 
print_sequence($fifth_frame_seq, 70); 
print "\n------------------------Reading Frame 6---------------------\n\n"; 
print_sequence($sixth_frame_seq, 70); 

##################################################### 
# Subroutines 
##################################################### 
# print_sequence 
# A subroutine to format and print sequence 
sub print_sequence { 
	my ($sequence, $length) = @_; 
	# Print sequence in lines of $length long 
	for (my $pos = 0; $pos < length($sequence); $pos += $length) { 
		print substr($sequence, $pos, $length), "\n"; 
	} 
}

#Now find another reading frame 5 nucleotides in


#How about an automated way of searching for the next reading frame?

$_ =  "The food is under the bar in the barn.";
print "orig = $_\n";
    if ( /foo(.*)bar/ ) {
        print "1 got <$1>\n";
        print "+ got <$+>\n";
        print "& got <$&>\n";
        print "' got <$'>\n";
        print "` got <$`>\n";
    }

#Several special variables also refer back to portions of the previous match. 
# $+ returns whatever the last bracket match matched. 
# $& returns the entire matched string.  
# $` returns everything before the matched string. 
# $' returns everything after the matched string. 

#Now implement a way of searching for the next open reading frame

